package cam.School.DataAccessObjectImpl;

import static cam.School.DataAccessObject.DAOUtil.close;
import static cam.School.DataAccessObject.DAOUtil.prepareStatement;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cam.School.DAOException.DAOException;
import cam.School.DataAccessObject.DAOFactory;
import cam.School.DataAccessObject.InfrastructurCategoryDAO;
import cam.School.DataTransfertObject.InfrastructurCategoryDTO;

public class InfrastructurCategoryDAOImpl implements InfrastructurCategoryDAO {
	
	// Constants ----------------------------------------------------------------------------------
	
    private static final String QUERY_PARAMS_GET_ALL ="id, name, type";
      
    private static final String UPDATE_QUERY_PARAMS ="name = ?, type = ?";
    
    private static final String SQL_FIND_ALL =
        "SELECT "+ QUERY_PARAMS_GET_ALL +" FROM infrastructurcategory WHERE type <> 'Class'";
    
    private static final String SQL_FIND_BY_NAME =
            "SELECT "+ QUERY_PARAMS_GET_ALL +" FROM infrastructurcategory WHERE name = ?";
        
    private static final String SQL_FIND_CLASS =
    	"SELECT "+ QUERY_PARAMS_GET_ALL +" FROM infrastructurcategory WHERE type = 'Class'";
    
    private static final String SQL_UPDATE_BY_ID =
        	"UPDATE classroom SET "+ UPDATE_QUERY_PARAMS +" FROM infrastructurcategory WHERE id = ?";
    
    
    private static final String SQL_INSERT =
        "INSERT INTO infrastructurcategory (name, type) VALUES (?, ?)";
    
    private static final String SQL_DELETE =
        "DELETE FROM infrastructurcategory WHERE id = ?";

    // Vars ---------------------------------------------------------------------------------------

    private DAOFactory daoFactory;

    // Constructors -------------------------------------------------------------------------------

    
    
    private static InfrastructurCategoryDTO map(ResultSet resultSet) throws SQLException {
    	InfrastructurCategoryDTO infrastructurCategory = new InfrastructurCategoryDTO();
    	infrastructurCategory.setId(resultSet.getLong("id"));
               
    	infrastructurCategory.setName(resultSet.getString("name"));
    	infrastructurCategory.setType(resultSet.getString("type"));
        return infrastructurCategory;
    }
    
    /**
     * Construct an User DAO for the given DAOFactory. Package private so that it can be constructed
     * inside the DAO package only.
     * @param daoFactory The DAOFactory to construct this InfrastructurCategoryDTO DAO for.
     */
    public InfrastructurCategoryDAOImpl(DAOFactory daoFactory) {
        this.daoFactory = daoFactory;
    }

    public List<InfrastructurCategoryDTO> getAll(String sqlQuery, Object... values){
    	Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<InfrastructurCategoryDTO> infrastructurCategories = new ArrayList<InfrastructurCategoryDTO>();

        try {
            connection = daoFactory.getConnection();
            
            if(values.length == 0 ){
				preparedStatement = connection.prepareStatement(sqlQuery);
			}else{
				preparedStatement = prepareStatement(connection, sqlQuery, false, values);
			}
            
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
            	infrastructurCategories.add(map(resultSet));
            }
        } catch (SQLException e) {
            throw new DAOException(e);
        } finally {
            close(connection, preparedStatement, resultSet);
        }

        return infrastructurCategories;
	}
    
	@Override
    public List<InfrastructurCategoryDTO> getAllClasses()throws DAOException {
		Object[] values = {};
		return getAll(SQL_FIND_CLASS, values);        
    }

	@Override
	public InfrastructurCategoryDTO getById(Long id) throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void create(InfrastructurCategoryDTO infrastructurCategory)
			throws IllegalArgumentException, DAOException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(InfrastructurCategoryDTO infrastructurCategory)
			throws IllegalArgumentException, DAOException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean delete(int id) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public InfrastructurCategoryDTO getByType(String type) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public InfrastructurCategoryDTO getByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<InfrastructurCategoryDTO> getAllCategory() {
		Object[] values = {};
		return getAll(SQL_FIND_ALL, values); 
	}
	
	
	
	@Override
	public InfrastructurCategoryDTO getCategoryByName(String categoryName) {
		Object[] values = {categoryName};
		if(getAll(SQL_FIND_BY_NAME, values) == null){
			return null;
		}
		return getAll(SQL_FIND_BY_NAME, values).get(0); 
	}
}
